﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Espam.Idiomas.Decimo.Tesis.Negocio;
using Microsoft.Reporting.WebForms;

namespace Espam.Idiomas.Decimo.Tesis.Web
{
    public partial class UI_Reprobados : System.Web.UI.Page
    {
        string validaSession = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {

                Response.Cache.SetCacheability(HttpCacheability.ServerAndNoCache);
                Response.Cache.SetAllowResponseInBrowserHistory(false);
                Response.Cache.SetNoStore();

                if (Session["userEspamIdiomasVarSession001"] == null || Session["rolEspamIdiomasVarSession003"] == null)
                {
                    validaSession = "NoHaIniciadoSession";
                    Response.Redirect("UI_Login.aspx?validaSession=" + validaSession);
                }
                else if (Session["userEspamIdiomasVarSession001"] != null && Session["rolEspamIdiomasVarSession003"].ToString() == "Docente")
                {
                    Response.Redirect("UI_DashboardTeacher.aspx");
                }
                else if (Session["userEspamIdiomasVarSession001"] != null && Session["rolEspamIdiomasVarSession003"].ToString() == "Administradores")
                {

                    MaintainScrollPositionOnPostBack = true;
                    btnGenerarReporte.Text = "Generar Reporte";
                    CargarCiclos();
                    ddlCiclo.AutoPostBack = true;

                }
                else if (Session["userEspamIdiomasVarSession001"] != null && Session["rolEspamIdiomasVarSession003"].ToString() == "Alumno")
                {
                    Response.Redirect("UI_DashboardStudents.aspx");
                }
            }
            else
            {
                MaintainScrollPositionOnPostBack = true;
            }
        }

        protected void btnGenerarReporte_Click(object sender, EventArgs e)
        {
            if (btnGenerarReporte.Text=="Generar Reporte")
            {
                CargaGrid();
            }
            else
            {
                btnGenerarReporte.Text = "Generar Reporte";
                CrearReporte();
            }
        }
        private void CargaGrid()
        {
            Usuarios oUsuarios = new Usuarios();
            List<Usuarios> lista = new List<Usuarios>();
            oUsuarios.ciclo = int.Parse(ddlCiclo.SelectedItem.Text);
            lista = oUsuarios.GetReprobados();
            if (lista.Count>0)
            {
                btnGenerarReporte.Text = "Exportar a pdf";
            }
            else
            {
                btnGenerarReporte.Text = "Generar Reporte";
            }
            gvReprobados.DataSource = lista;
            gvReprobados.DataBind();
            if (gvReprobados.Rows.Count > 0)
                ScriptManager.RegisterStartupScript(this, this.GetType(), "Dttable", String.Format("CargaDataTable('{0}',{1});", "Contenido_gvReprobados", gvReprobados.Rows[0].Cells.Count), true);
            
        }
        private void CargarCiclos()
        {
            Ciclos oCiclos = new Ciclos();
            List<Ciclos> lista = new List<Ciclos>();
            lista = oCiclos.GetCiclos();
            lista.Insert(0, new Ciclos() { idCiclo = 0, ciclo = 0 });
            ddlCiclo.DataSource = lista;
            ddlCiclo.DataTextField = "ciclo";
            ddlCiclo.DataValueField = "idCiclo";
            ddlCiclo.DataBind();
        }
        private void CrearReporte()
        {
            Usuarios oUsu = new Usuarios();

            EspamIdiomasDataSet.sp_getReprobadosDataTable dt = new EspamIdiomasDataSet.sp_getReprobadosDataTable();
            EspamIdiomasDataSetTableAdapters.sp_getReprobadosTableAdapter da = new EspamIdiomasDataSetTableAdapters.sp_getReprobadosTableAdapter();
            da.Fill(dt, int.Parse(ddlCiclo.SelectedItem.Text));

            ReportViewer viewer = new ReportViewer();
            ReportDataSource RD = new ReportDataSource();
            RD.Value = dt;
            RD.Name = "reprobados";
            //viewer.LocalReport.ReportPath = Server.MapPath("Reports/Administradores/Reprobados.rdlc");
            viewer.LocalReport.DataSources.Clear();
            viewer.LocalReport.DataSources.Add(RD);
            //viewer.LocalReport.ReportEmbeddedResource = "Reprobados.rdlc";
            viewer.LocalReport.ReportPath = Server.MapPath("Reports/Administradores/Reprobados.rdlc");
            viewer.LocalReport.Refresh();

            string mimeType;
            string encoding;
            string fileNameExtension;
            string[] streams;
            Warning[] warnings;
            byte[] pdfContent = viewer.LocalReport.Render("PDF", string.Empty, out mimeType, out encoding, out fileNameExtension, out streams, out warnings);

            this.Response.Clear();
            this.Response.ContentType = "application/pdf";
            this.Response.AddHeader("Content-disposition", "attachment; filename=Reprobados.pdf");
            this.Response.BinaryWrite(pdfContent);
            this.Response.End();

        }

        protected void ddlCiclo_SelectedIndexChanged(object sender, EventArgs e)
        {
            CargaGrid();
        }

        protected void gvReprobados_Load(object sender, EventArgs e)
        {
            if (gvReprobados.Rows.Count > 0)
                ScriptManager.RegisterStartupScript(this, this.GetType(), "Dttable", String.Format("CargaDataTable('{0}',{1});", "Contenido_gvReprobados", gvReprobados.Rows[0].Cells.Count), true);

        }

        protected void gvReprobados_PreRender(object sender, EventArgs e)
        {
            GridView gvReprobados = (sender) as GridView;
            if (gvReprobados.Rows.Count > 0)
            {
                //This replaces <td> with <th> and adds the scope attribute
                gvReprobados.UseAccessibleHeader = true;

                //This will add the <thead> and <tbody> elements
                gvReprobados.HeaderRow.TableSection = TableRowSection.TableHeader;

                //This adds the <tfoot> element. 
                //Remove if you don't have a footer row
                gvReprobados.FooterRow.TableSection = TableRowSection.TableFooter;
            }
        }
    }
}